import FeedbackDtlApi from '../../../../public/js/common/api/ctn/feedbackdetials.api.js'
import doT from '../../../vendor/template/doT.js';
import { requestFilePath } from '../../../../public/js/common/api/file-request.js'


(function (window, document, $) {
    'use strict';
    $('body').attr('data-weui-theme', 'light'); // 强制光明模式
    let _template = doT.template($('#cgdetails-template').text());

    $(function () {
        _initCommand();
    });
    // 命令初始化
    const _initCommand = () => {
        if (Number(getUrlKey("type")) === 1) {
            FeedbackDtlApi.proposalDtls(getUrlKey("id")).then(res => {
                // $('#cg-details').empty().append(_template(res));
                let detailsdata = res
                let imageArr = []
                if (Utils.IsNotNull(res.attachments)) {
                    res.attachments.forEach(item => {
                        FeedbackDtlApi.requestFilePath(item.id).then(res => {
                            imageArr.push({ url: res, id: item.id })
                            let alldata = { ...{ imageArr: imageArr }, ...detailsdata }
                            $('#cg-details').empty().append(_template(alldata));
                        })
                    });
                }
                else {
                    $('#cg-details').empty().append(_template(res));
                }
            })
        }
        else {
            FeedbackDtlApi.complaintDtls(getUrlKey("id")).then(res => {
                let detailsdata = res
                let imageArr = []
                if (Utils.IsNotNull(res.attachments)) {
                    res.attachments.forEach(item => {
                        FeedbackDtlApi.requestFilePath(item.id).then(res => {
                            imageArr.push({ url: res, id: item.id })
                            let alldata = { ...{ imageArr: imageArr }, ...detailsdata }
                            $('#cg-details').empty().append(_template(alldata));
                        })
                    });
                }
                else {
                    $('#cg-details').empty().append(_template(res));
                }
            })
        }
        $(document).on('click', '.picture_query img', function () {
            const id = $(this).attr('img-Id');
            requestFilePath(id).then(res => {
                $.photoBrowser({
                    items: [res.data]
                }).open();
                // style = "min-height:100vh;"
            })
        })
        new Swiper('.swiper-container', {
            direction: 'horizontal', // 水平切换选项
            autoplay: true,
            // 如果需要分页器
            pagination: {
                el: '.swiper-pagination',
                clickable: true,
            },
            observer: true,
            observeParents: true,
        });
    }
    /** 获取地址栏的 参数  传入 字段返回值 */
    const getUrlKey = (name) => {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

})(window, document, jQuery)